package com.bai.serviceUtil.erro;

import com.bai.common.Result;
import com.bai.common.ResultCodeEnum;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * 异常处理类
 */
@ControllerAdvice
public class GlobalExcrptionHandler {


    //全局异常处理，执行的方法
    @ExceptionHandler(Exception.class)
    @ResponseBody//返回json数据
    public Result error(Exception e) {
        e.printStackTrace();//异常的输出
        return Result.fail().message("全局异常处理....");
    }

    // 特定异常处理
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e) {
        e.printStackTrace();
        return Result.fail().message("特定异常处理....");
    }


    /**
     * 权限异常
     * @param e
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Result error(AccessDeniedException e){
        return Result.fail().code(205).message("没有操作权限");
    }

    //自定义异常
    @ExceptionHandler(BaiExcetion.class)
    @ResponseBody
    public Result error(BaiExcetion e) {
        e.printStackTrace();
        return Result.fail().code(e.getCode()).message(e.getMsg());
    }

}
